/**
 * Copyright (c) 2014-2017, huli.com
 * All rights reserved.
 */
package com.xiaohujr.xuexue.vm.exceptions;

import sun.misc.Unsafe;

import java.lang.reflect.Field;

/**
 * 本机直接内存溢出.
 *
 * @author Xue Xue (xuexue1@huli.com)
 * @version $Id$
 * @since 2018/5/3
 */
public class DirectMemoryOOM {
    private static final int _1MB = 1024 * 1024;

    /**
     * VM Args: -Xmx20M -XX:MaxDirectMemorySize=10M
     *
     * @param args
     * @throws IllegalAccessException
     */
    public static void main(String[] args) throws IllegalAccessException {
        Field unsafeFeild = Unsafe.class.getDeclaredFields()[0];
        unsafeFeild.setAccessible(true);
        Unsafe unsafe = (Unsafe) unsafeFeild.get(null);
        while (true) {
            unsafe.allocateMemory(_1MB);
        }
    }
}
